#####################################################################################
#
# 使用 SQLAlchemy 查询数据库
#
#####################################################################################

from models import *
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

# 连接SQLServer
engine = create_engine('mssql+pymssql://wluser:wlpwd.123@msdbt/wldbtest', echo=True)

with Session(engine) as session:
    spongebob = User(
        name="spongebob",
        fullname="Spongebob Squarepants",
        addresses=[Address(email_address="spongebob@sqlalchemy.org")],
    )

    sandy = User(
        name="sandy",
        fullname="Sandy Cheeks",
        addresses=[
            Address(email_address="sandy@sqlalchemy.org"),
            Address(email_address="sandy@squirrelpower.org"),
        ],
    )

    patrick = User(name="patrick", fullname="Patrick Star")

    session.add_all([spongebob, sandy, patrick])

    session.commit()
